import { createRouter, createWebHistory } from 'vue-router'
import {authService} from '@/service/AuthService'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/main', name: "main", component: () => import('@/views/Main.vue'), meta:{ requiresAuth: true } },
    { path: '/login', name: "login", component: () => import('@/views/Login.vue') },
    { path: "/", redirect: "/main"}
  ],
})

router.beforeEach((to, from) => {
  if (to.meta.requiresAuth && !authService.isLoggedIn) {
    return {name: "login"};
  }
})

export default router
